package com.dfec.water.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dfec.component.uid.core.service.UidService;
import com.dfec.framework.mybatis.core.page.PageParam;
import com.dfec.framework.mybatis.core.page.PageUtils;
import com.dfec.water.dao.YdbAreaBaseInfoDao;
import com.dfec.water.dao.YdbAreaDeviceDao;
import com.dfec.water.dao.YdbAreaImgDao;
import com.dfec.water.dao.YdbDeviceTypeDao;
import com.dfec.water.entity.*;
import com.dfec.water.param.AreaAddParam;
import com.dfec.water.service.YdbAreaBaseInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author trg
 * @title: YdbAreaBaseInfoServiceImpl
 * @description: 区域选择Service业务层处理
 * @date Thu Jan 09 10:57:17 CST 2025
 */
@Service("ydbAreaBaseInfoService")
@RequiredArgsConstructor
public class YdbAreaBaseInfoServiceImpl extends ServiceImpl<YdbAreaBaseInfoDao, YdbAreaBaseInfoEntity> implements YdbAreaBaseInfoService {

    private final YdbAreaBaseInfoDao ydbAreaBaseInfoDao;

    private final UidService uidService;

    private final YdbAreaDeviceDao areaDeviceDao;

    private final YdbDeviceTypeDao ydbDeviceTypeDao;

    private final YdbAreaImgDao imgDao;

    /**
     * 分页查询
     *
     * @param pageParam 入参
     * @return PageUtils
     * @author trg
     * @date Thu Jan 09 10:57:17 CST 2025
     */
    @Override
    public PageUtils queryPage(PageParam<YdbAreaBaseInfoEntity> pageParam) {
        return ydbAreaBaseInfoDao.selectPage(pageParam);
    }

    /**
     * 不带分页查询
     *
     * @param ydbAreaBaseInfoEntity 入参
     * @return List<YdbAreaBaseInfoEntity>
     * @author trg
     * @date Thu Jan 09 10:57:17 CST 2025
     */
    @Override
    public List<YdbAreaBaseInfoEntity> listAll(YdbAreaBaseInfoEntity ydbAreaBaseInfoEntity) {
        List<YdbAreaBaseInfoEntity> ydbAreaBaseInfoEntities = ydbAreaBaseInfoDao.selectAll(ydbAreaBaseInfoEntity);
        List<YdbAreaImgEntity> ydbAreaImgEntities = imgDao.listAllByAreaIds(ydbAreaBaseInfoEntities.stream().map(YdbAreaBaseInfoEntity::getId).collect(Collectors.toList()));
        if (ydbAreaImgEntities.isEmpty()) {
            return ydbAreaBaseInfoEntities;
        }
        Map<Long, List<YdbAreaImgEntity>> collect = ydbAreaImgEntities.stream().collect(Collectors.groupingBy(YdbAreaImgEntity::getAreaId));

        ydbAreaBaseInfoEntities.forEach(item -> {
            item.setImgList(collect.getOrDefault(item.getId(), null));
        });

        return ydbAreaBaseInfoEntities;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveArea(AreaAddParam param) {
        YdbAreaBaseInfoEntity ydbAreaBaseInfoEntity = new YdbAreaBaseInfoEntity();
        BeanUtils.copyProperties(param, ydbAreaBaseInfoEntity);
        long next = uidService.next();
        ydbAreaBaseInfoEntity.setId(next);
        return super.save(ydbAreaBaseInfoEntity);
    }

    @Override
    public void authDevice(AreaAddParam param) {
        List<YdbAreaDeviceEntity> ydbAreaDeviceEntities = new ArrayList<>();
        for (YdbDeviceTypeEntity deviceType : param.getDeviceItems()) {
            YdbAreaDeviceEntity ydbAreaDeviceEntity = new YdbAreaDeviceEntity();
            ydbAreaDeviceEntity.setId(uidService.next());
            ydbAreaDeviceEntity.setAreaId(param.getId());
            ydbAreaDeviceEntity.setDeviceId(deviceType.getId());
            ydbAreaDeviceEntity.setDeviceType(deviceType.getDevType());
            ydbAreaDeviceEntities.add(ydbAreaDeviceEntity);
        }
        areaDeviceDao.insertBatch(ydbAreaDeviceEntities);
    }

    @Override
    public List<YdbAreaDeviceEntity> getAuthDevice(Long id) {
        YdbAreaDeviceEntity ydbAreaDeviceEntity = new YdbAreaDeviceEntity();
        ydbAreaDeviceEntity.setAreaId(id);
        return areaDeviceDao.selectList(areaDeviceDao.dealParams(ydbAreaDeviceEntity));

    }


    @Override
    public List<YdbDeviceTypeDictEntity> getAreaDevices(Long areaId) {
        List<YdbDeviceTypeDictEntity> ydbDeviceTypeDictEntities = areaDeviceDao.selectByareaId(areaId);
        return ydbDeviceTypeDictEntities;
    }

    @Override
    public List<YdbDeviceTypeEntity> selectByAreaId(Long areaId,Integer devType){
        return areaDeviceDao.selectDeviceALlByAreaId(areaId,devType);
    }
}